package com.retail.order.service.impl;

import com.retail.common.core.domain.Result;
import com.retail.common.core.utils.DateUtils;
import com.retail.order.domain.TOrder;
import com.retail.order.dto.OrderDetailResponse;
import com.retail.order.mapper.TOrderMapper;
import com.retail.order.service.ITOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单Service业务层处理
 * 
 * @author retail
 * @date 2024-02-27
 */
@Service
public class TOrderServiceImpl implements ITOrderService 
{
    @Autowired
    private TOrderMapper tOrderMapper;

    /**
     * 查询订单
     * 
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public TOrder selectTOrderByOrderId(Long orderId)
    {
        return tOrderMapper.selectTOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表
     * 
     * @param tOrder 订单
     * @return 订单
     */
    @Override
    public List<TOrder> selectTOrderList(TOrder tOrder)
    {
        return tOrderMapper.selectTOrderList(tOrder);
    }

    /**
     * 新增订单
     * 
     * @param tOrder 订单
     * @return 结果
     */
    @Override
    public int insertTOrder(TOrder tOrder)
    {
        tOrder.setCreateTime(DateUtils.getNowDate());
        return tOrderMapper.insertTOrder(tOrder);
    }

    /**
     * 修改订单
     * 
     * @param tOrder 订单
     * @return 结果
     */
    @Override
    public int updateTOrder(TOrder tOrder)
    {
        tOrder.setUpdateTime(DateUtils.getNowDate());
        return tOrderMapper.updateTOrder(tOrder);
    }

    /**
     * 批量删除订单
     * 
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteTOrderByOrderIds(Long[] orderIds)
    {
        return tOrderMapper.deleteTOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     * 
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteTOrderByOrderId(Long orderId)
    {
        return tOrderMapper.deleteTOrderByOrderId(orderId);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @Override
    public Result cancelOrder(Long orderId) {
        int i = tOrderMapper.cancelOrder(orderId);
        if (i>0){
            return Result.error("取消订单失败");
        }
        return Result.success("取消订单成功");
    }

    @Override
    public Result updateRemake(TOrder tOrder) {
        int i = tOrderMapper.updateRemake(tOrder);
        if (tOrder.getOrderStatus()!=1){
            return Result.error("订单已发货，不能修改地址");
        }else {
            if (i<0){
                return Result.error("修改地址失败");
            }
        }
        return Result.success("修改地址成功");
    }

    @Override
    public OrderDetailResponse selectByOderCode(String orderCode) {
        return tOrderMapper.selectByOderCode(orderCode);
    }

    @Override
    public Map<Integer, List<TOrder>> selectTOrderMergeOrderList(List<OrderBatchService.OrderRequest> orderReqs) {
        // 提取订单请求中的所有用户ID
        List<Integer> userIds = orderReqs.stream().map(OrderBatchService.OrderRequest::getUserId).collect(Collectors.toList());

        // 查询订单列表
        List<TOrder> orderList = tOrderMapper.selectOrderMergeList(userIds);

        // 创建一个用于存储按用户ID分组订单列表的Map
        Map<Integer, List<TOrder>> orderMap = new HashMap<>();

        // 遍历订单列表，按用户ID分组
        for (TOrder tOrder : orderList) {
            Integer userId = tOrder.getUserId();
            orderMap.computeIfAbsent(userId, k -> new ArrayList<>()).add(tOrder);
        }

        // 打印结果
        System.out.println(orderMap.toString ());
        System.out.println(orderList.toString ());

        return orderMap;
    }


}
